package com.threegroup.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.threegroup.admin.dao.SysRoleDataScopeDao;
import com.threegroup.admin.dao.SysRoleMenuDao;
import com.threegroup.admin.dao.SysRoleUserDao;
import com.threegroup.admin.dto.SysRoleDTO;
import com.threegroup.admin.dao.SysRoleDao;
import com.threegroup.admin.entity.SysRoleEntity;
import com.threegroup.admin.query.SysRoleQuery;
import com.threegroup.admin.service.ISysRoleMenuService;
import com.threegroup.admin.service.ISysRoleService;
import com.threegroup.admin.service.ISysRoleDataScopeService;
import com.threegroup.common.constant.Constant;
import com.threegroup.common.utils.BeanCopierUtils;
import com.threegroup.common.utils.RequestHolder;
import com.threegroup.common.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色 Service 实现类
 */
@Service
public class SysRoleServiceImpl implements ISysRoleService {

    @Resource
    private SysRoleDao roleDao;

    @Resource
    private SysRoleUserDao sysRoleUserDao;

    @Resource
    private SysRoleMenuDao sysRoleMenuDao;

    @Resource
    private SysRoleDataScopeDao sysRoleDataScopeDao;

    @Resource
    private ISysRoleMenuService sysRoleMenuService;

    @Resource
    private ISysRoleDataScopeService sysRoleDataScopeService;

    /**
     * 根据用户id获取角色信息
     * @param userId 用户id
     * @return
     */
    @Override
    public SysRoleEntity selectByUserId(Long userId) {

        return roleDao.selectByUserId(userId);
    }

    /**
     * 获取角色列表
     * @return
     */
    @Override
    public List<SysRoleDTO> list() {

        List<SysRoleEntity> roleEntityList = roleDao.selectList();

        List<SysRoleDTO> sysRoleDTOS = BeanCopierUtils.copyList(roleEntityList, SysRoleDTO.class);

        return sysRoleDTOS;
    }

    /**
     * 分页查询
     * @param sysRoleQuery
     * @return
     */
    @Override
    public Result<Map<String, Object>> getRoleList(SysRoleQuery sysRoleQuery) {
        PageHelper.startPage(sysRoleQuery.getPage(), sysRoleQuery.getLimit());

        List<SysRoleEntity> sysRoleEntities = roleDao.selectRoleList(sysRoleQuery);
        PageInfo<SysRoleEntity> pageInfo = new PageInfo<SysRoleEntity>(sysRoleEntities);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total",pageInfo.getTotal());

        return new Result<Map<String, Object>>().ok(map);
    }

    /**
     * 获取角色信息
     * @param id
     * @return
     */
    @Override
    public SysRoleDTO getRoleInfoById(Long id) {

        SysRoleEntity sysRoleEntity = roleDao.selectById(id);

        List<Long> menuList = sysRoleMenuDao.getMenuIdList(id);
        List<Long> deptId = sysRoleDataScopeDao.getDeptIdList(id);

        SysRoleDTO sysRoleDto = BeanCopierUtils.copy(sysRoleEntity, SysRoleDTO.class);
        sysRoleDto.setMenuIdList(menuList);
        sysRoleDto.setDeptIdList(deptId);

        return sysRoleDto;
    }

    /**
     * 保存角色信息
     * @param sysRoleDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRoleDTO sysRoleDTO) {

        HttpServletRequest request = RequestHolder.getRequest();
        Long userId = Long.parseLong(request.getAttribute(Constant.USER_ID).toString());

        SysRoleEntity entity = BeanCopierUtils.copy(sysRoleDTO, SysRoleEntity.class);
        entity.setUpdater(userId);
        entity.setUpdateDate(new Date());

        //更新角色
        roleDao.updateById(entity);

        //更新角色菜单关系
        sysRoleMenuService.saveOrUpdate(entity.getId(), sysRoleDTO.getMenuIdList());

        //更新角色数据权限关系
        sysRoleDataScopeService.saveOrUpdate(entity.getId(), sysRoleDTO.getDeptIdList());

    }
}
